package com.dianrui.qiyouriding.activity;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.dianrui.qiyouriding.Constant;
import com.dianrui.qiyouriding.R;
import com.dianrui.qiyouriding.util.GlideUtil;
import com.dianrui.qiyouriding.okhttp.OkHttpRequest;
import com.dianrui.qiyouriding.util.NoDoubleClickUtils;
import com.dianrui.qiyouriding.util.SharedUtil;
import com.dianrui.qiyouriding.util.StringUtils;
import com.dianrui.qiyouriding.util.ToastUtil;
import com.dianrui.qiyouriding.util.Url;
import com.google.gson.JsonObject;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 按左上角进入我的界面
 */
public class MyActivity extends BaseActivity {
    @BindView(R.id.pic)
    ImageView pic;
    @BindView(R.id.my_nickname)
    TextView nickname;

    @BindView(R.id.click_partner)
    RelativeLayout clickParnter;
    @BindView(R.id.partner_line)
    TextView partnerLine;

    @Override
    public int getContentViewId() {
        return R.layout.activity_mymenus;
    }

    @Override
    public void initView() {
        customInit(true, R.color.main_color);
        if (!StringUtils.isEmpty(SPUtils.getInstance().getString("face"))) {
            GlideUtil.loadUserImageViewSize(getApplicationContext(), SPUtils.getInstance().getString("face"), pic);
        }
        if (!StringUtils.isEmpty(SPUtils.getInstance().getString("nickname"))) {
            nickname.setText(SPUtils.getInstance().getString("nickname"));
        }

    }

    @Override
    public void initData() {}


    @Override
    protected void onResume() {
        super.onResume();
        getUserInfo();
    }

    public void getUserInfo() {
        JsonObject jsonObject = new JsonObject();
        if (!StringUtils.isEmpty(SPUtils.getInstance().getString("member_id"))) {
            jsonObject.addProperty("member_id", SPUtils.getInstance().getString("member_id"));
        }
        OkHttpRequest.getInstance().postRequests(Url.USERINFO, jsonObject.toString(), new OkHttpRequest.XCallBack() {

            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        final JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && !isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("status"))) {
                                if (jsonObject.optJSONObject("data").has("face")) {
                                    GlideUtil.loadUserImageViewSize(getApplicationContext(), jsonObject.optJSONObject("data").optString("face"), pic);
                                }
                                if (jsonObject.optJSONObject("data").has("nickname")) {
                                    nickname.setText(jsonObject.optJSONObject("data").optString("nickname"));
                                }

                                if (jsonObject.optJSONObject("data").optInt("is_open") == 1) {
                                    clickParnter.setVisibility(View.VISIBLE);
                                    partnerLine.setVisibility(View.VISIBLE);
                                } else {
                                    clickParnter.setVisibility(View.GONE);
                                    partnerLine.setVisibility(View.GONE);
                                }

                                SPUtils.getInstance().put("is_partner", jsonObject.optJSONObject("data").optString("is_partner"));
                                SPUtils.getInstance().put("token", jsonObject.optJSONObject("data").optString("token"), true);
                                SPUtils.getInstance().put("member_token", jsonObject.optJSONObject("data").optString("member_token"), true);

                            }else  if (Constant.BACKLOGIN.equals(jsonObject.optString("status"))) {
                                SPUtils.getInstance().clear();
                                JumpKillActivitys(LoginActivity.class);
                            }

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void failedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });
    }





    @OnClick({R.id.back, R.id.pic, R.id.click_credit_score, R.id.click_score_shop, R.id.click_partner, R.id.click_customer_center, R.id.click_more, R.id.click_my_activity, R.id.click_my_coupon, R.id.click_my_vetion, R.id.my_order, R.id.my_wallet, R.id.click_setting})
    public void onViewClicked(View view) {
        if (NoDoubleClickUtils.isDoubleClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            //头像
            case R.id.pic:
                startActivity(new Intent(MyActivity.this, PersonInfoActivity.class));
                break;
            //活动列表
            case R.id.click_my_activity:
                startActivity(new Intent(MyActivity.this, WebviewBuyCouponActivity.class));
                break;
            case R.id.click_credit_score:

                break;
            //我的优惠劵
            case R.id.click_my_coupon:
                startActivity(new Intent(MyActivity.this, CouponWebviewActicity.class));
                break;
            //违章记录
            case R.id.click_my_vetion:
                startActivity(new Intent(MyActivity.this, ViolationListActivity.class));

                break;
            //我的行程
            case R.id.my_order:
                startActivity(new Intent(MyActivity.this, MyOrderActivity.class));
                break;
            //我的钱包
            case R.id.my_wallet:
                startActivity(new Intent(MyActivity.this, MyWalletActivity.class));
                break;
            case R.id.click_partner://合伙人计划
                if (!StringUtils.isEmpty(SPUtils.getInstance().getString("is_partner"))) {
                    if (SPUtils.getInstance().getString("is_partner").equals("0")) {
                        startActivity(new Intent(MyActivity.this, PartnerWebViewActivity.class));
                    } else {
                        startActivity(new Intent(MyActivity.this, PartnerPlanActivity.class));
                    }
                }
                break;
            //设置
            case R.id.click_setting:
                startActivity(new Intent(MyActivity.this, SettingsActivity.class));
                break;
            //更多
            case R.id.click_more:
                startActivity(new Intent(MyActivity.this, MoreActivity.class));
                break;
            //信用积分
            case R.id.click_score_shop:
                startActivity(new Intent(MyActivity.this, ShopScoreWebviewActicity.class));
                break;
            //客服中心
            case R.id.click_customer_center:
                startActivity(new Intent(MyActivity.this, CustomerWebViewActivity.class));
                break;
            default:
                break;
        }
    }
}
